package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.ActorAssRoleDao;
import cn.edu.sdjzu.xg.bysj.domain.authority.Actor;
import cn.edu.sdjzu.xg.bysj.domain.authority.ActorAssRole;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public final class ActorAssRoleService {
    //获取ActorAssRoleDao实例
    private static final ActorAssRoleDao actorAssRoleDao = ActorAssRoleDao.getInstance();
    //新建ActorAssRoleService对象
    private static final ActorAssRoleService actorAssRoleService = new ActorAssRoleService();
    //构造器
    private ActorAssRoleService() {
    }
    //获取 ActorAssRoleService实例
    public static ActorAssRoleService getInstance() {
        return actorAssRoleService;
    }

    /**
     * 获得某Actor对象关联的所有ActorAssRole对象
     *
     * @param actor
     * @return
     * @throws SQLException
     */
    public Collection<ActorAssRole> findAll(Actor actor) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        Collection<ActorAssRole> actorAssRoles=null;
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        try {
            actorAssRoles = actorAssRoleDao.findAll(connection, actor);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //关闭连接
            JdbcHelper.close(connection);
        }
        return actorAssRoles;
    }

    /**
     * 获得某Actor对象关联的所有ActorAssRole对象
     *
     * @return
     * @throws SQLException
     */
    public Collection<ActorAssRole> findAll() throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Collection<ActorAssRole> actorAssRoles = null;
        try {
            actorAssRoles = actorAssRoleDao.findAll(connection);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return actorAssRoles;
    }

    /**
     * 获得某Actor对象关联的所有ActorAssRole对象
     *
     * @param id
     * @return
     * @throws SQLException
     */
    public ActorAssRole find(Integer id) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        ActorAssRole actorAssRole = null;
        try {
            actorAssRole = actorAssRoleDao.find(connection, id);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return actorAssRole;

    }

    /**
     * 获得某Actor对象关联的所有ActorAssRole对象
     *
     * @param actorAssRole
     * @return
     * @throws SQLException
     */
    public boolean update(ActorAssRole actorAssRole) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        boolean updated = false;
        try {
            updated = actorAssRoleDao.update(connection, actorAssRole);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return updated;
    }

    /**
     * 获得某Actor对象关联的所有ActorAssRole对象
     *
     * @param id
     * @return
     * @throws SQLException
     */

    public boolean delete(Integer id) throws SQLException {
        boolean deleted = false;
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        try {
            deleted = actorAssRoleDao.delete(connection, id);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return deleted;
    }
}

